﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;  // 这里库中有String的扩展方法
using JKF.Entities.Model;
using JKF.BusinessLayer.BLL; 
using JKF.DataModule.SqlServerDAL;
using JKF.BusinessLayer;
using JKF.Entities;
using System.Data;  // 必须引用此命名空间，否则GetDic_Levels方法出错
using JKF.DataModule;

namespace JKF.HandlerLayer.Customer
{
    public partial  class Dic_LevelHandler:AHttpHandler
    {
        private static bllDIC_LEVEL bllDic_Level = new bllDIC_LEVEL();
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }


        #region operate
        /// <summary>
        /// 添加会员级别
        /// </summary>
        public void InsertLevel()
        {
            try
            {
                var strScr = P("moduleObj");
                var dic_LevelInfo = this.BuildDIC_LEVELinfoByJson(JsonSerializer.Deserialize<DIC_LEVELTempinfo>(strScr), false);
                var tid = dic_LevelInfo.LEVEL_CODE;
                bllDic_Level.Add(dic_LevelInfo);
                var pk = dic_LevelInfo.LEVEL_CODE;
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "添加成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 修改会员级别名称
        /// </summary>
        public void EditLevel()
        {
            try
            {
                var strScr = P("moduleObj");
                var dic_LevelInfo = BuildDIC_LEVELinfoByJson(JsonSerializer.Deserialize<DIC_LEVELTempinfo>(strScr), true);
                var tid = dic_LevelInfo.LEVEL_CODE;
                bllDic_Level.Update(dic_LevelInfo);
                var pk = dic_LevelInfo.LEVEL_CODE;
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "修改成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 删除会员级别名称
        /// </summary>
        public void DeleteDic_Level()
        {
            try
            {
                var level_Code = P("id");
                //bllDic_Level.Delete(level_Code);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = bllDic_Level.TransactionSQLDeleteBLL(level_Code), Data = level_Code }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 获取店面列表数据
        /// </summary>
        public void GetDic_Levels()
        {
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in bllDic_Level.GetDic_LevelsBLL(P("NAME"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             LEVEL_CODE = a.Field<string>("LEVEL_CODE"),
                             NAME = a.Field<string>("NAME"), 
                             PRODUCT_DISCOUNT = SetDiscountStr((a.Field<string>("PRODUCT_DISCOUNT"))??""),
                             POINT_DISCOUNT = SetDiscountStr((a.Field<string>("POINT_DISCOUNT"))??""),
                             MEMO = a.Field<string>("MEMO")
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }
        /// <summary>
        /// 根据模块level_code获得实体模块信息
        /// </summary>
        public void GetLevelByCode()
        {
            try
            {
                var scid = P("moduleKey");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllDic_Level.GetModel(scid) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        } 
        #endregion

        #region Action
        /// <summary>
        /// 从Json对象构建SYS_MODULE
        /// </summary>
        /// <param name="shopInfo">实体对象</param>
        /// <param name="isEdit">是否为编辑</param>
        /// <returns></returns>
        public DIC_LEVELinfo BuildDIC_LEVELinfoByJson(DIC_LEVELTempinfo dic_LevelInfo, bool isEdit)
        {//SHOP_CODE, SHOP_NAME, LINKMAN, PHONE, AREA_CODE, ADDRESS, POSTAL_CODE, MEMO, SMS_AVAIBLE, SMS_SENDED, SINGNATURE, USER_CREATE, DATE_CREATE, DATE_UPDATE
            var entity = new DIC_LEVELinfo();
            if (!isEdit) // 添加
            {
                entity.LEVEL_CODE = this.AutoBuildLevelCode();
            }
            else // 修改
            {
                entity.LEVEL_CODE = dic_LevelInfo.LEVEL_CODE;
            }
            entity.NAME = dic_LevelInfo.NAME;
            entity.PRODUCT_DISCOUNT = Convert.ToDecimal(dic_LevelInfo.PRODUCT_DISCOUNT);
            entity.POINT_DISCOUNT = Convert.ToDecimal(dic_LevelInfo.POINT_DISCOUNT);
            entity.MEMO = dic_LevelInfo.MEMO;
            entity.USER_CREATE = bllSYS_USER.CurrentUser.USER_KEY; // 获取当前用户guid
            entity.DATE_CREATE = System.DateTime.Now;
            return entity;
        }
        /// <summary>
        /// 自动生成会员级别编码
        /// </summary>
        /// <returns></returns>
        private string AutoBuildLevelCode()
        {
            return bllDic_Level.GetLevelCodeBLL();
        }

        /// <summary>
        /// 处理折扣率显示
        /// </summary>
        /// <param name="discount"></param>
        /// <returns></returns>
        private string SetDiscountStr(string discount)
        {
            if (string.IsNullOrEmpty(discount))
            {
                return string.Empty;
            }
            if (discount == "1.00")
            {
                return "不折扣";
            } 
            return Convert.ToDouble(discount)*10+"折";
        }
        #endregion
        

        /// <summary>
        /// 检查已输入名称是否存在
        /// </summary>
        public void CheckLevelName()
        {
            try
            {
                var level_name = P("LEVEL_NAME");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllDic_Level.ExistsLevelNameBLL(level_name) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        #region Class
        public class DIC_LEVELTempinfo
        {
            public string LEVEL_CODE;
            public string NAME;
            public string POINT_DISCOUNT;
            public string PRODUCT_DISCOUNT;
            public string MEMO;
            public string DISPLAY_ORDER;
        } 
        #endregion
    }

    
}
